From c20efddd5eee01bb5db0aeaac8fc0b64abf6d6cf Mon Sep 17 00:00:00 2001
From: Mario Ceresa <mrceresa@gmail.com>
Date: Sun, 20 Mar 2011 14:27:14 +0100
Subject: [PATCH 4/6] Use system charls

---
 CMake/3rdparty.cmake          |   17 +++++++++++++++++
 CMake/FindCharLS.cmake        |   34 ++++++++++++++++++++++++++++++++++
 CMakeLists.txt                |    1 +
 dcmjpls/apps/CMakeLists.txt   |    2 +-
 dcmjpls/libsrc/CMakeLists.txt |    2 +-
 5 files changed, 54 insertions(+), 2 deletions(-)
 create mode 100644 CMake/FindCharLS.cmake

diff --git a/CMake/3rdparty.cmake b/CMake/3rdparty.cmake
index 74ce78f..36956ce 100755
--- a/CMake/3rdparty.cmake
+++ b/CMake/3rdparty.cmake
@@ -197,6 +197,23 @@ ELSE(WIN32)
       SET(WITH_ZLIB 1)
     ENDIF(NOT ZLIB_LIBS)
   ENDIF(DCMTK_WITH_ZLIB)
+  
+  # Find charls
+  
+  SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake)
+  
+  IF(DCMTK_WITH_CHARLS)
+    FIND_PACKAGE(CharLS)
+    INCLUDE_DIRECTORIES(${CHARLS_INCLUDE_DIRS})
+    SET(CHARLS_LIBS ${CHARLS_LIBRARIES})
+    IF (NOT CHARLS_LIBS)
+      MESSAGE(STATUS "Warning: CharLS not found. Cannot build!")
+    ELSE(NOT CHARLS_LIBS)
+      MESSAGE(STATUS "Info: DCMTK CHARLS support will be enabled")
+      SET(WITH_CHARLS 1)
+    ENDIF(NOT CHARLS_LIBS)
+  ENDIF(DCMTK_WITH_CHARLS)
+  
 
 ENDIF(WIN32)
 
diff --git a/CMake/FindCharLS.cmake b/CMake/FindCharLS.cmake
new file mode 100644
index 0000000..eaa45b7
--- /dev/null
+++ b/CMake/FindCharLS.cmake
@@ -0,0 +1,34 @@
+FIND_PATH(CHARLS_INCLUDE_DIR CharLS/interface.h
+/usr/local/include
+/usr/include
+)
+
+FIND_LIBRARY(CHARLS_LIBRARY
+  NAMES CharLS
+  PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64
+  )
+
+
+IF (CHARLS_LIBRARY AND CHARLS_INCLUDE_DIR)
+    SET(CHARLS_LIBRARIES ${CHARLS_LIBRARY})
+    SET(CHARLS_INCLUDE_DIRS ${CHARLS_INCLUDE_DIR})
+    SET(CHARLS_FOUND "YES")
+ELSE (CHARLS_LIBRARY AND CHARLS_INCLUDE_DIR)
+  SET(CHARLS_FOUND "NO")
+ENDIF (CHARLS_LIBRARY AND CHARLS_INCLUDE_DIR)
+
+
+IF (CHARLS_FOUND)
+   IF (NOT CHARLS_FIND_QUIETLY)
+      MESSAGE(STATUS "Found CHARLS: ${CHARLS_LIBRARIES}")
+   ENDIF (NOT CHARLS_FIND_QUIETLY)
+ELSE (CHARLS_FOUND)
+   IF (CHARLS_FIND_REQUIRED)
+      MESSAGE(FATAL_ERROR "Could not find CHARLS library")
+   ENDIF (CHARLS_FIND_REQUIRED)
+ENDIF (CHARLS_FOUND)
+
+MARK_AS_ADVANCED(
+  CHARLS_LIBRARIES
+  CHARLS_INCLUDE_DIR
+  )
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 86b7601..fa5d9ad 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -36,6 +36,7 @@ SET(DCMTK_PACKAGE_VERSION_NUMBER ${DCMTK_MAJOR_VERSION}${DCMTK_MINOR_VERSION}${D
 
 
 # DCMTK build options
+OPTION(DCMTK_WITH_CHARLS "Configure DCMTK with support for CHARLS" ON)
 OPTION(DCMTK_WITH_TIFF "Configure DCMTK with support for TIFF" ON)
 OPTION(DCMTK_WITH_PNG "Configure DCMTK with support for PNG" ON)
 OPTION(DCMTK_WITH_XML "Configure DCMTK with support for XML" OFF)
diff --git a/dcmjpls/apps/CMakeLists.txt b/dcmjpls/apps/CMakeLists.txt
index d993bc2..45abcb9 100644
--- a/dcmjpls/apps/CMakeLists.txt
+++ b/dcmjpls/apps/CMakeLists.txt
@@ -16,6 +16,6 @@ ENDFOREACH(PROGRAM)
 
 # make sure executables are linked to the corresponding libraries
 FOREACH(PROGRAM dcmcjpls dcmdjpls dcml2pnm)
-  TARGET_LINK_LIBRARIES(${PROGRAM} dcmjpls charls dcmimage dcmimgle dcmdata oflog ofstd ofstd ${ZLIB_LIBS} ${THREAD_LIBS} ${WIN32_STD_LIBRARIES})
+  TARGET_LINK_LIBRARIES(${PROGRAM} dcmjpls ${CHARLS_LIBS} dcmimage dcmimgle dcmdata oflog ofstd ofstd ${ZLIB_LIBS} ${THREAD_LIBS} ${WIN32_STD_LIBRARIES})
 ENDFOREACH(PROGRAM)
 TARGET_LINK_LIBRARIES(dcml2pnm ${LIBTIFF_LIBS} ${LIBPNG_LIBS})
diff --git a/dcmjpls/libsrc/CMakeLists.txt b/dcmjpls/libsrc/CMakeLists.txt
index 63a2766..9499dd8 100644
--- a/dcmjpls/libsrc/CMakeLists.txt
+++ b/dcmjpls/libsrc/CMakeLists.txt
@@ -1,5 +1,5 @@
 # declare additional include directories
-INCLUDE_DIRECTORIES(${dcmjpls_SOURCE_DIR}/include ${ofstd_SOURCE_DIR}/include ${oflog_SOURCE_DIR}/include ${dcmdata_SOURCE_DIR}/include ${dcmimgle_SOURCE_DIR}/include ${dcmimage_SOURCE_DIR}/include ${dcmjpls_SOURCE_DIR}/libcharls ${ZLIB_INCDIR})
+INCLUDE_DIRECTORIES(${dcmjpls_SOURCE_DIR}/include ${ofstd_SOURCE_DIR}/include ${oflog_SOURCE_DIR}/include ${dcmdata_SOURCE_DIR}/include ${dcmimgle_SOURCE_DIR}/include ${dcmimage_SOURCE_DIR}/include ${CHARLS_INCLUDE_DIRS} ${ZLIB_INCDIR})
 
 # create library from source files
 ADD_LIBRARY(dcmjpls djcparam djdecode djencode djrparam djcodecd djutils djcodece)
-- 
1.7.4

